package org.example.rowing.service;

import lombok.extern.slf4j.Slf4j;
import org.example.rowing.domain.*;
import org.example.rowing.vo.RaceVO;
import org.example.rowing.vo.RoundScoreVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ProcessElevenToTwelveBoatsService {

    @Autowired
    private ResultInfoService resultInfoService;
    @Autowired
    private RaceInfoService raceInfoService;
    @Autowired
    private RaceProcessCommonService raceProcessCommonService;

    public List<RoundScoreVO> getAdvancingToSemis(List<RoundScoreVO> p1List, List<RoundScoreVO> p2List, Long raceId) {
        List<RoundScoreVO> list1 = raceProcessCommonService.sortedEntries(p1List);
        List<RoundScoreVO> list2 = raceProcessCommonService.sortedEntries(p2List);
        List<RoundScoreVO> advancingToSemis = new ArrayList<>();
        List<RoundScoreVO> tmpList1 = new ArrayList<>();
        List<RoundScoreVO> tmpList2 = new ArrayList<>();
        // 按照第一种复赛方案进行
        for (int i = 1; i < list1.size(); i++) {
            if (i % 2 == 1) {
                tmpList1.add(list1.get(i));
            } else {
                tmpList1.add(list2.get(i));
            }
        }

        for (int i = 1; i < list2.size(); i++) {
            if (i % 2 == 1) {
                tmpList2.add(list2.get(i));
            } else {
                tmpList2.add(list1.get(i));
            }
        }
        // 前6个进入复赛A，其余的进入复赛B
        advancingToSemis.addAll(tmpList1);
        advancingToSemis.addAll(tmpList2);
        return advancingToSemis;
    }

    public List<RoundScoreVO> semToFinals(List<RoundScoreVO> s1List, List<RoundScoreVO> s2List, Long raceId) {
        // 获取复赛晋级决赛的队伍
        List<RoundScoreVO> list1 = raceProcessCommonService.sortedEntries(s1List);
        List<RoundScoreVO> list2 = raceProcessCommonService.sortedEntries(s2List);
        List<RoundScoreVO> advancingToFinals = new ArrayList<>();
        List<RoundScoreVO> tmpList = new ArrayList<>();
        // 决赛A的队伍
        advancingToFinals.add(list2.get(0));
        advancingToFinals.add(list1.get(0));
        advancingToFinals.add(list1.get(1));
        advancingToFinals.add(list2.get(1));
        // 决赛B的队伍
        int min = Math.min(list1.size(), list2.size());
        for (int i = 2; i < min; i++) {
            if (i % 2 == 0) {
                tmpList.add(list1.get(i));
                tmpList.add(list2.get(i));
            } else {
                tmpList.add(list2.get(i));
                tmpList.add(list1.get(i));
            }
        }
        for (int i = min; i < list1.size(); i++) {
            tmpList.add(list1.get(i));
        }
        for (int i = min; i < list2.size(); i++) {
            tmpList.add(list2.get(i));
        }
        // 整合，前四名晋级决赛A，后面的晋级决赛B
        advancingToFinals.addAll(tmpList);

        return advancingToFinals;
    }

    public List<RoundScoreVO> getAdvancingToFinals(List<RoundScoreVO> p1List, List<RoundScoreVO> p2List, Long raceId) {
        if (p2List != null) {
            // 获取预赛直接晋级决赛的队伍
            // 获取复赛名单时已排过序，此时无序排序
            List<RoundScoreVO> advancingToFinals = new ArrayList<>();
            advancingToFinals.add(p1List.get(0));
            advancingToFinals.add(p2List.get(0));
            return advancingToFinals;
        }
        // 获取复赛晋级决赛的队伍
        return raceProcessCommonService.sortedEntries(p1List);
    }

    public List<RoundScoreVO> getRank(List<RoundScoreVO> f1List, List<RoundScoreVO> f2List, RaceVO raceVO) {
        List<RoundScoreVO> list = raceProcessCommonService.sortedEntries(f1List);
        List<RoundScoreVO> list1 = raceProcessCommonService.sortedEntries(f2List);
        list.addAll(list1);
        saveFinalResult(list, raceVO);
        int i = 0;
        for (RoundScoreVO roundScoreVO : list) {
            Double v = Double.valueOf(i++);
            RoundScore roundScore = new RoundScore(raceVO.getId(), "比赛结果", null, null, v, roundScoreVO.getRegistrationVO().getRegistrationId(), roundScoreVO.getStatus());
            raceInfoService.saveRoundScore(roundScore);
        }
        return list;
    }

    private void saveFinalResult(List<RoundScoreVO> finalList, RaceVO raceVO) {
        raceProcessCommonService.saveFinalResult(finalList, raceVO);
    }
}
